<?php
/*
 * @Author: qwl qwl@ncmem.com
 * @Date: 2022-08-18 17:39:17
 * @LastEditors: qwl qwl@ncmem.com
 * @LastEditTime: 2022-11-10 15:03:24
 * @FilePath: \up6\db\store\fastdfs\FastDFSWriter.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
namespace store\fastdfs;

use Eelly\FastDFS\Client;
use utils\ConfigReader;
use store\FileBlockWriter;
use store\StorageType;
use utils\FileTool;
use utils\PathTool;
use utils\SysTool;

/*
	FastDFS块写入器
	手动添加方法
	  修改FastDFS手动添加
		public function uploadAppendFile($path,$ext){
			list($storageInfo, $storage) = $this->getRandomStorage();
			$result = $storage->uploadAppenderFile($storageInfo['storage_index'], $path, $ext);
			return $result['group'].'/'.$result['path'];
		}
		public function appendFile($id,$data){
			list($groupName, $filePath) = explode('/', $id, 2);
			$result = $this->getStorage($groupName)->appendFile($data, $filePath);
			return $result['group'].'/'.$result['path'];
		}
	更新记录：
	  2022-11-10 
	  2020-05-14 创建。
*/
class FastDFSWriter extends FileBlockWriter
{
	function __construct() 
	{        
		$this->storage = StorageType::FastDFS;
	}

	/**
	 * @description: 创建文件,f_create.php调用
	* @param {*} $file
	* @return {*}
	*/
	function make($file)
	{
	    $data=null;
	    FileTool::writeAll($file->pathSvr, $data);
        $c = new Client(ConfigReader::fastDFSConfig());
        $id = $c->uploadAppendFile($file->pathSvr,PathTool::getExtention($file->pathSvr));
		return $id;
	}
	
	/**
	 * 向文件中写入块数据
	 * @param long $blockOffset
	 * @param long $lenLoc
	 * @param string $pathSvr
	 * @param string $tmpFile
	 */
	function write($file,$data)
	{   
		$client = new Client(ConfigReader::fastDFSConfig());
		$client->appendFile($file->object_id,$data);
	}
	
	/**
	 * 向文件中写入块数据
	 * @param long $blockOffset
	 * @param long $lenLoc
	 * @param string $pathSvr
	 * @param string $tmpFile
	 */
	function writeData($blockOffset,$lenLoc,$pathSvr,$data)
	{
	    //写入数据
	    $sys = new SysTool();
	    $pathSvr = $sys->to_gbk($pathSvr);
	    $hfile = fopen($pathSvr,"r+b");
	    //定位到续传位置
	    fseek($hfile,intval($blockOffset),SEEK_SET);
	    fwrite($hfile,$data);
	    fclose($hfile);
	}
	function writeLastPart(/*FileInf*/$file){}
}
?>